<?php
  require_once "function.php";
  $targetdate=getdate(time()-180*24*3600);
  $currentdate=getdate();

  $url1="http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/PatientMedicalRecordListQuery.aspx?SESSION=".$sessionid;

  $hidden=viewstate($url1);
  $r=new HttpRequest($url1);
  $r->setMethod(HTTP_METH_POST);
  $data=array('scrollLeft'=>'','scrollTop'=>'','__LASTFOCUS'=>'','NTUHWeb1$ChartInputTextBox'=>$_GET['id'],'__EVENTTARGET'=>'','__EVENTARGUMENT'=>'','__VIEWSTATE'=>$hidden['viewstate'],'__EVENTVALIDATION'=>$hidden['eventvalidation'],'NTUHWeb1$ButtonQuery'=>'查詢');
  $r->addPostFields($data);
  $r->send();
  $a=$r->getResponseBody();

  $dom=new DOMDocument;
  $dom->perserveWhiteSpace=FALSE;
  @$dom->loadHTML($a);
  foreach($dom->getElementsByTagName('input') as $element){
      if($element->getAttribute('name')=='NTUHWeb1$PatAccountListRecord1$personidHidden') $ID_no=$element->getAttribute('value');
  }
  $history="http://ihisaw.ntuh.gov.tw/WebApplication/OtherIndependentProj/PatientBasicInfoEdit/ReportResultQuery.aspx?SESSION=".$sessionid."&PatClass=I&PersonID=".$ID_no."&Hosp=T0&DefaultQuery=EMRContent";

  $hidden2=viewstate($history);
  $r2=new HttpRequest($history);
  $r2->setMethod(HTTP_METH_POST);
  $data2=array('__EVENTTARGET'=>'ShowReport','__EVENTARGUMENT'=>'','__VIEWSTATE'=>$hidden2['viewstate'],'__EVENTVALIDATION'=>$hidden2['eventvalidation'],'DateTextBoxYearMonthDayStart$YearInput'=>$targetdate['year'],'DateTextBoxYearMonthDayStart$MonthInput'=>$targetdate['mon'],'DateTextBoxYearMonthDayStart$DayInput'=>$targetdate['mday'],'DateTextBoxYearMonthDayEnd$YearInput'=>$currentdate['year'],'DateTextBoxYearMonthDayEnd$MonthInput'=>$currentdate['mon'],'DateTextBoxYearMonthDayEnd$DayInput'=>$currentdate['mday']);
  $r2->addPostFields($data2);
  $r2->send();
  $a2=$r2->getResponseBody();
//  echo $a2;

  $labDom=new DOMDocument();
  $labDom->preserveWhiteSpace=FALSE;
  $labDom->encoding='utf-8';
  @$labDom->loadHTML($a2);
  foreach($labDom->getElementsByTagName('table') as $idx => $element){
      if($idx==1) echo utf8_decode($labDom->saveXML($element));
  }

?>
